/*
 * EnigmaView.java
 * Objetivo: Formulario Principal de la interfaz gráfica de la aplicación.
 * Última fecha de modificación: 25 de Mayo de 2010
 * Autor: David Cabañero Vázquez.
 */

package enigma;

/**************** IMPORTACIÓN DE LIBRERÍAS ************/

import java.io.UnsupportedEncodingException;
import java.security.*;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.*;
import java.net.*;
import java.security.spec.InvalidKeySpecException;

/**************** ESPECIFICACIÓN DE LA CASE ************/

public class EnigmaView extends FrameView {

   /* Definición de los atributos de la clase*/
    private comunicacion c;
    private seguridad comSegura;
    private boolean conectado = false;
    private boolean preferenciasGuardadas = false;

    
    public EnigmaView(SingleFrameApplication app) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
        super(app);
        c = new comunicacion();
        initComponents();
    }

    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = EnigmaApp.getApplication().getMainFrame();
            aboutBox = new EnigmaAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        EnigmaApp.getApplication().show(aboutBox);
    }


    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelbackground = new javax.swing.JPanel();
        panelPrinciapal = new javax.swing.JPanel();
        pestanas = new javax.swing.JTabbedPane();
        panelChat = new javax.swing.JPanel();
        botonEnviar = new javax.swing.JButton();
        scrollPanelVisualizar = new javax.swing.JScrollPane();
        listaVisualizar = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        textoEnviar = new javax.swing.JTextArea();
        panelPreferencias = new javax.swing.JPanel();
        textoAlias = new javax.swing.JTextField();
        etiquetaAlias = new javax.swing.JLabel();
        etiquetaPass = new javax.swing.JLabel();
        textoPuerto = new javax.swing.JTextField();
        etiquetaPuerto = new javax.swing.JLabel();
        textoIP = new javax.swing.JTextField();
        etiquetaIP = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        textoPass = new javax.swing.JPasswordField();
        botonConectar = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        panelbackground.setName("panelbackground"); // NOI18N
        panelbackground.setPreferredSize(new java.awt.Dimension(450, 574));

        panelPrinciapal.setName("panelPrinciapal"); // NOI18N

        pestanas.setName("pestanas"); // NOI18N

        panelChat.setName("panelChat"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(enigma.EnigmaApp.class).getContext().getActionMap(EnigmaView.class, this);
        botonEnviar.setAction(actionMap.get("enviarMensaje")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(enigma.EnigmaApp.class).getContext().getResourceMap(EnigmaView.class);
        botonEnviar.setText(resourceMap.getString("botonEnviar.text")); // NOI18N
        botonEnviar.setName("botonEnviar"); // NOI18N

        scrollPanelVisualizar.setName("scrollPanelVisualizar"); // NOI18N

        listaVisualizar.setBorder(new javax.swing.border.MatteBorder(null));
        listaVisualizar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        listaVisualizar.setName("listaVisualizar"); // NOI18N
        scrollPanelVisualizar.setViewportView(listaVisualizar);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        textoEnviar.setColumns(20);
        textoEnviar.setRows(3);
        textoEnviar.setToolTipText(resourceMap.getString("textoEnviar.toolTipText")); // NOI18N
        textoEnviar.setAutoscrolls(true);
        textoEnviar.setBorder(new javax.swing.border.LineBorder(resourceMap.getColor("textoEnviar.border.lineColor"), 1, true)); // NOI18N
        textoEnviar.setName("textoEnviar"); // NOI18N
        jScrollPane1.setViewportView(textoEnviar);

        javax.swing.GroupLayout panelChatLayout = new javax.swing.GroupLayout(panelChat);
        panelChat.setLayout(panelChatLayout);
        panelChatLayout.setHorizontalGroup(
            panelChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelChatLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollPanelVisualizar, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelChatLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(botonEnviar, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        panelChatLayout.setVerticalGroup(
            panelChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelChatLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollPanelVisualizar, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addComponent(botonEnviar, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
                .addContainerGap())
        );

        pestanas.addTab(resourceMap.getString("panelChat.TabConstraints.tabTitle"), panelChat); // NOI18N

        panelPreferencias.setName("panelPreferencias"); // NOI18N

        textoAlias.setText(resourceMap.getString("textoAlias.text")); // NOI18N
        textoAlias.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        textoAlias.setName("textoAlias"); // NOI18N

        etiquetaAlias.setText(resourceMap.getString("etiquetaAlias.text")); // NOI18N
        etiquetaAlias.setName("etiquetaAlias"); // NOI18N

        etiquetaPass.setText(resourceMap.getString("etiquetaPass.text")); // NOI18N
        etiquetaPass.setName("etiquetaPass"); // NOI18N

        textoPuerto.setText(resourceMap.getString("textoPuerto.text")); // NOI18N
        textoPuerto.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        textoPuerto.setName("textoPuerto"); // NOI18N

        etiquetaPuerto.setText(resourceMap.getString("etiquetaPuerto.text")); // NOI18N
        etiquetaPuerto.setName("etiquetaPuerto"); // NOI18N

        textoIP.setText(resourceMap.getString("textoIP.text")); // NOI18N
        textoIP.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        textoIP.setName("textoIP"); // NOI18N

        etiquetaIP.setText(resourceMap.getString("etiquetaIP.text")); // NOI18N
        etiquetaIP.setName("etiquetaIP"); // NOI18N

        jButton1.setAction(actionMap.get("guardarPreferencias")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        textoPass.setText(resourceMap.getString("textoPass.text")); // NOI18N
        textoPass.setName("textoPass"); // NOI18N

        botonConectar.setAction(actionMap.get("Conectar")); // NOI18N
        botonConectar.setText(resourceMap.getString("botonConectar.text")); // NOI18N
        botonConectar.setName("botonConectar"); // NOI18N

        javax.swing.GroupLayout panelPreferenciasLayout = new javax.swing.GroupLayout(panelPreferencias);
        panelPreferencias.setLayout(panelPreferenciasLayout);
        panelPreferenciasLayout.setHorizontalGroup(
            panelPreferenciasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPreferenciasLayout.createSequentialGroup()
                .addGroup(panelPreferenciasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelPreferenciasLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(panelPreferenciasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(etiquetaPuerto)
                            .addComponent(etiquetaIP)))
                    .addGroup(panelPreferenciasLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(etiquetaAlias))
                    .addGroup(panelPreferenciasLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(etiquetaPass)))
                .addGap(24, 24, 24)
                .addGroup(panelPreferenciasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textoAlias, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(panelPreferenciasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(textoIP, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                        .addComponent(textoPuerto, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(textoPass, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE))
                .addGap(227, 227, 227))
            .addGroup(panelPreferenciasLayout.createSequentialGroup()
                .addGap(207, 207, 207)
                .addGroup(panelPreferenciasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(botonConectar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(226, Short.MAX_VALUE))
        );
        panelPreferenciasLayout.setVerticalGroup(
            panelPreferenciasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPreferenciasLayout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(panelPreferenciasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textoIP, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(etiquetaIP))
                .addGap(42, 42, 42)
                .addGroup(panelPreferenciasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textoPuerto, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(etiquetaPuerto))
                .addGap(31, 31, 31)
                .addGroup(panelPreferenciasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(etiquetaAlias)
                    .addComponent(textoAlias, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(panelPreferenciasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(etiquetaPass)
                    .addComponent(textoPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(botonConectar)
                .addGap(28, 28, 28))
        );

        pestanas.addTab(resourceMap.getString("panelPreferencias.TabConstraints.tabTitle"), panelPreferencias); // NOI18N

        javax.swing.GroupLayout panelPrinciapalLayout = new javax.swing.GroupLayout(panelPrinciapal);
        panelPrinciapal.setLayout(panelPrinciapalLayout);
        panelPrinciapalLayout.setHorizontalGroup(
            panelPrinciapalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPrinciapalLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pestanas, javax.swing.GroupLayout.PREFERRED_SIZE, 515, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelPrinciapalLayout.setVerticalGroup(
            panelPrinciapalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPrinciapalLayout.createSequentialGroup()
                .addComponent(pestanas, javax.swing.GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE)
                .addGap(88, 88, 88))
        );

        javax.swing.GroupLayout panelbackgroundLayout = new javax.swing.GroupLayout(panelbackground);
        panelbackground.setLayout(panelbackgroundLayout);
        panelbackgroundLayout.setHorizontalGroup(
            panelbackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelbackgroundLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelPrinciapal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        panelbackgroundLayout.setVerticalGroup(
            panelbackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelbackgroundLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelPrinciapal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        jMenuItem1.setAction(actionMap.get("showPreferences")); // NOI18N
        jMenuItem1.setName("preferencesMenuItem"); // NOI18N
        fileMenu.add(jMenuItem1);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(panelbackground);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

/*
 * Objetivo: Manejador de la acción de envío del mensaje.
 * Autor: David Cabañero Vázquez.
 */
    @Action
    public void enviarMensaje() throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException {
        if(conectado)
            c.Enviar(c.getAlias() + ": " + textoEnviar.getText());
        else
            JOptionPane.showMessageDialog(null, new String("El sistema no está conectado correctamente"), "fus fus!", JOptionPane.ERROR_MESSAGE);
        textoEnviar.setText("");

    }


/*
 * Objetivo: Manejador de la acción de conexión al grupo.
 * Autor: David Cabañero Vázquez.
 */
    @Action
    public void Conectar() throws NoSuchAlgorithmException
        {
        if (preferenciasGuardadas)
        {
        MulticastSocket socket = c.Conectar();
        if (socket != null)
           JOptionPane.showMessageDialog(null, new String("La conexión se ha realizado con éxito"), "Conectado!", JOptionPane.INFORMATION_MESSAGE);
        else
           JOptionPane.showMessageDialog(null, new String("El sistema no ha podido establecer conexión"), "Error de COnexión", JOptionPane.ERROR_MESSAGE);

        hilos actualizarInterfaz = new hilos(c,listaVisualizar);
        Thread hilo = new Thread (actualizarInterfaz);
        hilo.start();
        conectado = true;
        }
        else
            JOptionPane.showMessageDialog(null, new String("Debe guardar sus preferencias antes de conectar al canal"), "No ha guardado sus preferencias!", JOptionPane.ERROR_MESSAGE);
        }

/*
 * Objetivo: Manejador de la acción de guardado de preferencias.
 * Autor: David Cabañero Vázquez.
 */
    @Action
    public void guardarPreferencias() throws NoSuchAlgorithmException
        {
        c.setGrupo(textoIP.getText());
        c.setAlias(textoAlias.getText());
        c.setPass(textoPass.getText());
        c.setPuerto(Integer.parseInt(textoPuerto.getText()));
        c.cambiarPass();
        JOptionPane.showMessageDialog(null, new String("Sus datos se han guardado con éxito"), "Datos Guardados", JOptionPane.INFORMATION_MESSAGE);
        preferenciasGuardadas = true;
        }

 

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonConectar;
    private javax.swing.JButton botonEnviar;
    private javax.swing.JLabel etiquetaAlias;
    private javax.swing.JLabel etiquetaIP;
    private javax.swing.JLabel etiquetaPass;
    private javax.swing.JLabel etiquetaPuerto;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listaVisualizar;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JPanel panelChat;
    private javax.swing.JPanel panelPreferencias;
    private javax.swing.JPanel panelPrinciapal;
    private javax.swing.JPanel panelbackground;
    private javax.swing.JTabbedPane pestanas;
    private javax.swing.JScrollPane scrollPanelVisualizar;
    private javax.swing.JTextField textoAlias;
    private javax.swing.JTextArea textoEnviar;
    private javax.swing.JTextField textoIP;
    private javax.swing.JPasswordField textoPass;
    private javax.swing.JTextField textoPuerto;
    // End of variables declaration//GEN-END:variables


    private JDialog aboutBox;
}